package com.arpa.wms.controller.wms;


import cn.hutool.core.util.IdUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.cache.PartyCache;
import com.arpa.ntocc.common.common.cache.ShipmentCache;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.ShipmentWarehouseValidate;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.dto.GoodsPnDTO;
import com.arpa.wms.domain.entity.GoodsPn;
import com.arpa.wms.domain.vo.GoodsPnVo;
import com.arpa.wms.service.IGoodsPnService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 商品PN码 前端控制器
 * </p>
 *
 * @author leo
 * @since 2021-08-24
 */
@Api(tags = "商品PN维护")
@RestController
@RequestMapping(value = "/goodspn", produces = "application/json;charset=UTF-8")
@Log4j2
public class GoodsPnController {

    @Autowired
    private IGoodsPnService goodsPnService;
    @Autowired
    private ShipmentCache shipmentCache;
    @Autowired
    private PartyCache partyCache;

    private static final String GOODSPN_NAME_MANAGE = "商品PN名称不能重复";
    private static final String GOODSPN_CODE_MANAGE = "商品PN编码不能重复";

    @ApiOperation(value = "获取商品PN列表", notes = "获取商品PN列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = GoodsPnVo.class))
    @NeedLogin
    @GetMapping
    public Result list(GoodsPnDTO dto, HttpServletRequest request) {
        try {
            //校验授权仓库code和货主code不能为空
            ShipmentWarehouseValidate.validateShipmentCodesAndWarehouseCodes();
            dto.setGroupCode(UserUtil.getBranchCode());
            //登录账号授权的货主过滤
            dto.setShipmentCodes(UserUtil.getAuthorizeShipmentCodes());
            GoodsPnVo voSum = goodsPnService.queryListSum(dto);
            //过滤所属机构
            voSum.setGroupCode(UserUtil.getBranchCode());
            List<GoodsPnVo> voList = goodsPnService.queryList(dto);

            Page<GoodsPnVo> returnPage = new Page(dto.getPageNum(), dto.getPageSize());
            returnPage.setRecords(voList);
            returnPage.setTotal(voSum.getTotal());
            returnPage.setSumObject(voSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 添加 承运商
     *
     * @param entity  Warehouse
     * @param request
     * @return
     */
    @ApiOperation(value = "添加商品PN", notes = "添加商品PN")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456")
    })
    @NeedLogin
    @PostMapping("/save")
    public Result create(@RequestBody GoodsPn entity, HttpServletRequest request) {

        long count = goodsPnService.count(new LambdaQueryWrapper<GoodsPn>().eq(GoodsPn::getShipmentCode, entity.getShipmentCode()).eq(GoodsPn::getPn, entity.getPn()));
        if (count > 0) {
            return Result.error( "pn码【" + entity.getPn() + "】已存在，不能重复添加！");
        }
        String creator = UserUtil.getCode();
        String creatorName = partyCache.translate(creator);
        entity.setCode(IdUtil.fastSimpleUUID());
        entity.setGroupCode(UserUtil.getBranchCode());
        entity.setCreatedBy(creator);
        entity.setCreatedName(creatorName);
        entity.setModifiedBy(creator);
        entity.setModifiedName(creatorName);
        entity.setShipmentName(shipmentCache.translate(entity.getShipmentCode()));
        goodsPnService.save(entity);
        return Result.ok("商品" + entity.getGoodsName() + "与" + entity.getPn() + "关系保存成功！");
    }


    @ApiOperation(value = "删除商品PN码", notes = "删除商品PN码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "code", value = "商品PN标识", paramType = "path", dataType = "String")
    })
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result remove(@PathVariable String code, HttpServletRequest request) {
        goodsPnService.remove(new LambdaQueryWrapper<GoodsPn>().eq(GoodsPn::getCode, code).eq(GoodsPn::getGroupCode,UserUtil.getBranchCode()));
        return Result.ok("删除成功");
    }
}
